package view.backend.product;

import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminProductSearch 
{
    private JPanel productSearchPanel;
    private AdminProduct adminProduct;
    private JTextField tFieldString;

    public AdminProductSearch(AdminProduct view)
    {
        adminProduct = view;
        
        productSearchPanel = new JPanel(null);
        productSearchPanel.setBounds(0, 0, 300, 80);
        productSearchPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Søg", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        productSearchPanel.setOpaque(false);
        
        JLabel lblName = new JLabel("Varenummer/Navn/Varegruppe");
        lblName.setBounds(10, 25, 280, 18);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.PLAIN, 12f));
        
        tFieldString = new JTextField();
        tFieldString.setBounds(10, 45, 280, 25);
        tFieldString.setBorder(DesignLayout.T_FIELD_RED);
        tFieldString.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldString.setFont(DesignLayout.STANDARD);
        tFieldString.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldString.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                doLookUp();
            }
        });
        
        productSearchPanel.add(lblName);
        productSearchPanel.add(tFieldString);
        
        productSearchPanel.setVisible(true);
    }
    
    public void reset()
    {
        tFieldString.setText("");
        adminProduct.getPrdList().buildExistingScrollPanel("", "ORDER BY productID DESC", "LIMIT 10");
    }
    
    private void doLookUp()
    {
        StringBuilder str = new StringBuilder();
        str.append("WHERE ");
        str.append("productName LIKE '%"+tFieldString.getText()+"%'");
        str.append(" OR ");
        str.append("ProductFamilyName LIKE '%"+tFieldString.getText()+"%'");
        str.append(" OR ");
        str.append("productID LIKE '%"+tFieldString.getText()+"%'");
        adminProduct.getPrdList().buildExistingScrollPanel(""+str, "ORDER BY productID DESC", "");        
    }

    public JPanel getPanel()
    {
        return productSearchPanel;
    }
}